package jdbc.datasource;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

/**
 * @author DCSGO
 * @version 1.0
 * druid的使用
 */
public class Druid_ {
    @Test
    public void testDruid() throws Exception {
        //1.添加 druid 连接池的 jar 包
        //2.添加 druid 连接池的配置文件到 src 目录下
        //3.创建连接池对象
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //4.获取连接
        long start = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            Connection connection = dataSource.getConnection();
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
    }
    /* 连接次数从 5000 -> 500000 耗时稍有变化
     * 十月 03, 2023 8:44:56 上午 com.alibaba.druid.pool.DruidDataSource info
     * 信息: {dataSource-1} inited
     * 耗时：566
     *
     * 进程已结束，退出代码为 0
     */
}
